package cn.signalpha.modules.base.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.modules.base.dto.NoticeTemplateDTO;
import cn.signalpha.modules.base.entity.NoticeTemplate;
import cn.signalpha.modules.base.service.NoticeTemplateService;
import cn.signalpha.modules.base.vo.NoticeTemplateVO;
import cn.signalpha.modules.base.wrapper.NoticeTemplateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/notice/template")
@Api(value = "消息通知模板", tags = "消息通知模板")
public class NoticeTemplateController extends BaseController {

	private final NoticeTemplateService noticeTemplateService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="type", dataType = "string", value = "类型"),
        @ApiImplicitParam(name="name", dataType = "string", value = "名称"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<NoticeTemplate>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<NoticeTemplate> wrapper = noticeTemplateService.selectNoticeTemplateWrapper(params);
        return R.data(noticeTemplateService.page(Condition.getPage(query), wrapper));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<NoticeTemplateVO> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(NoticeTemplateWrapper.build().entityVO(noticeTemplateService.getById(id)));
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入noticeTemplate")
	public R update(@Valid @RequestBody NoticeTemplateDTO noticeTemplateDTO) {
		NoticeTemplate noticeTemplate = noticeTemplateService.getById(noticeTemplateDTO.getId());
		Assert.isNull(noticeTemplate, MsgConstant.OBJECT_NULL);
		if(noticeTemplate.getIsMp() == -1){
			noticeTemplateDTO.setIsMp(-1);
			noticeTemplateDTO.setMpId(null);
		}
		if(noticeTemplate.getIsMa() == -1){
			noticeTemplateDTO.setIsMa(-1);
			noticeTemplateDTO.setMaId(null);
		}
		if(noticeTemplate.getIsSms() == -1){
			noticeTemplateDTO.setIsSms(-1);
			noticeTemplateDTO.setSmsId(null);
		}
		return R.status(noticeTemplateService.updateById(BeanUtil.copy(noticeTemplateDTO, NoticeTemplate.class)));
	}

}
